const Opticals = require("../model/opticals.model.js");

class OpticalsService {
  //保存公司信息，如果没有companyId则为新增，否则为更新
  async saveOpticals({
    opticalId,
    nodeName,
    linkName,
    nodeCode,
    nodeType,
    longitude,
    latitude,
    R,
    G,
    B,
    Z
  }) {
    var update = false;
    var opticals = {};
    var whereOpt = {};
    if (opticalId && opticalId > 0) {
      update = true;
      whereOpt.opticalId = opticalId;
    }
    nodeName && Object.assign(opticals, { nodeName });
    nodeCode && Object.assign(opticals, { nodeCode });
    nodeType && Object.assign(opticals, { nodeType });
    longitude && Object.assign(opticals, { longitude });
    latitude && Object.assign(opticals, { latitude });
    linkName && Object.assign(opticals, { linkName });
    Z && Object.assign(opticals, { Z });
    R && Object.assign(opticals, { R });
    G && Object.assign(opticals, { G });
    B && Object.assign(opticals, { B });
    if (Object.keys(opticals).length == 0) {
      return false;
    }
    var res;
    try {
      if (update) {
        res = await Opticals.update(opticals, { where: whereOpt });
        if (res[0] > 0) {
          opticals.opticalId = opticalId;
          return opticals;
        } else {
          return false;
        }
      } else {
        res = await Opticals.create(opticals);
        if (res.dataValues == undefined) {
          return false;
        } else {
          return res.dataValues;
        }
      }
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  //删除company
  async eraseOpticalById(opticalId) {
    if (!opticalId || opticalId <= 0) {
      return false;
    }
    var res;
    try {
      res = Opticals.destroy({ where: { opticalId } });
      if (!res) {
        return false;
      }
      return true;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  /* 
  删除指定条件的节点
  */
 async eraseOpticals({linkName,nodeType}){
  var whereOpt = {};
  linkName && Object.assign(whereOpt,{linkName});
  nodeType && Object.assign(whereOpt,{nodeType});
  if(Object.keys(whereOpt).length == 0){
    return false;
  }
  var res;
  try {
    res = await Opticals.destroy({where:whereOpt});
    if(!res){
      return false;
    }else{
      return true;
    }
  } catch (error) {
    console.log(error);
    return null;
  }
 }
  /* 
  批量添加
  */
  async  batchAddOpticals(arr){
    if(!(arr instanceof Array) || arr.length == 0){
      return false;
    }
    var res;
    try {
      res = await Opticals.bulkCreate(arr,{
        fields:[
          "nodeName",
          "nodeCode",
          "nodeType",
          "longitude",
          "latitude",
          "linkName",
          "R",
          "G",
          "B",
          "Z"
        ]
      });
      return res;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  /*
   *查找指定条件的公司
   *@page:分页选项
   *@page.pageIndex:当前第几页
   *@page.pageSize:每页多少条记录
   *@page.pageCount:总共多少页
   *@page.sum:总共多少条记录
   */
  async getOptical({ opticalId, nodeName, linkName }) {
    var whereOpt = {};
    opticalId >= 0 && Object.assign(whereOpt, { opticalId });
    nodeName && Object.assign(whereOpt, { nodeName });
    linkName && Object.assign(whereOpt, { linkName });
    var res;
    try {
      res = await Opticals.findAll({
        where: whereOpt,
        order: [["opticalId", "ASC"]],
      });
      if(!res || res == null){
        return [];
      }
      return JSON.parse(JSON.stringify(res));
    } catch (error) {
      console.log(error);
      return null;
    }
  }
}

module.exports = new OpticalsService();
